package com.examples.AppoointmentManager;

import java.util.Calendar;

import com.examples.Model.AppoinModel;
import com.examples.database.AppoinAdapter;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CalendarView;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.DatePicker.OnDateChangedListener;
import android.widget.EditText;
import android.widget.TimePicker;
import android.widget.Toast;

public class Reminder extends Activity implements OnClickListener{

	private DatePicker da3;
	private TimePicker ti3;
	private Button done;
	String rm1,rm2,rm3,rm4,rm5,date,time;
	Integer tempyear,tempmonth,tempday,temphour,tempmin;
	Context ctx;
//	Integer mYear,mMonth,mDay;
	private TextView mDateDisplay;
    private Button mPickDate;
	private int mYear;
	private int mMonth;
	private int mDay;
	private TextView mTimeDisplay;
    private Button mPickTime;

    private int mHour;
    private int mMinute;

        static final int TIME_DIALOG_ID = 0;
	
	    static final int DATE_DIALOG_ID = 1;
	
	
	@Override
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);    
        setContentView(R.layout.reminder);
        //done.setOnClickListener(this);
      ctx=this;
        final AppoinAdapter ap=new AppoinAdapter(ctx);   
      
        
        
      da3=(DatePicker)findViewById(R.id.datePicker1);
      
      //-----------------------------------------------------------------
   // capture our View elements
      mDateDisplay = (TextView) findViewById(R.id.dateDisplay);
      mPickDate = (Button) findViewById(R.id.pickDate);
     // capture our View elements
      mTimeDisplay = (TextView) findViewById(R.id.timeDisplay);
      mPickTime = (Button) findViewById(R.id.pickTime);
      // add a click listener to the button
      mPickTime.setOnClickListener(new View.OnClickListener() {
          public void onClick(View v) {
              showDialog(TIME_DIALOG_ID);
          }
      });
      // add a click listener to the button
      mPickDate.setOnClickListener(new View.OnClickListener() {
          public void onClick(View v) {
              showDialog(DATE_DIALOG_ID);
          }
      });

      // get the current date
      final Calendar c = Calendar.getInstance();
      mYear = c.get(Calendar.YEAR);
      mMonth = c.get(Calendar.MONTH);
      mDay = c.get(Calendar.DAY_OF_MONTH);

      // display the current date (this method is below)
      updateDisplay();
      // get the current time
      final Calendar d = Calendar.getInstance();
      mHour = d.get(Calendar.HOUR_OF_DAY);
      mMinute = d.get(Calendar.MINUTE);

      // display the current date
      updateDisplayTime();


      //-----------------------------------------------------------------
      /*tempyear=da3.getYear();  
      tempmonth=da3.getMonth()+1;
      tempday=da3.getDayOfMonth();
      
      date=tempday.toString()+"/"+tempmonth.toString()+"/"+tempyear.toString();
     */
      
      
      /*ti3=(TimePicker)findViewById(R.id.timePicker1);
      temphour=ti3.getCurrentHour();
      tempmin=ti3.getCurrentMinute();
      time=temphour.toString()+"/"+tempmin.toString();
      //Log.d("----","date");
      //Log.d("++++","time");
      */
      
      
      
      done=(Button)findViewById(R.id.addreminder);
        
       
      rm1=getIntent().getExtras().getString("ag2");
      rm2=getIntent().getExtras().getString("pe2");
      rm3=getIntent().getExtras().getString("de2");
      rm4=getIntent().getExtras().getString("da2");
      rm5=getIntent().getExtras().getString("ti2");
	
     // Log.d("test", rm1);
      done.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Toast t=Toast.makeText(Reminder.this, "Appointment Created", 5000);
				if(v == done){
					AppoinModel am=new AppoinModel(); 
					
					am.setAgenda(rm1);
					am.setPerson(rm2);
					am.setDescription(rm3);
					am.setDate(rm4);
					am.setTime(rm5);
					am.setReminderDate(date);
					am.setReminderTime(time);
					Log.d("----","++++");
					ap.open();
					ap.createAppointment(am);
					ap.close();

		        }
				
			}
		});
}
	
	 // updates the date in the TextView
    private void updateDisplay() {
        mDateDisplay.setText(
            new StringBuilder()
                    // Month is 0 based so add 1
                    .append(mMonth + 1).append("-")
                    .append(mDay).append("-")
                    .append(mYear).append(" "));
        date=mDateDisplay.getText().toString();
    }
    
 // updates the time we display in the TextView
    private void updateDisplayTime() {
        mTimeDisplay.setText(
            new StringBuilder()
                    .append(pad(mHour)).append(":")
                    .append(pad(mMinute)));
        time=mTimeDisplay.getText().toString();
    }

    private static String pad(int c) {
        if (c >= 10)
            return String.valueOf(c);
        else
            return "0" + String.valueOf(c);
    }
    
 // the callback received when the user "sets" the time in the dialog
    private TimePickerDialog.OnTimeSetListener mTimeSetListener =
        new TimePickerDialog.OnTimeSetListener() {
            public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                mHour = hourOfDay;
                mMinute = minute;
                updateDisplayTime();
            }
        };
    
        

       
        
    // the callback received when the user "sets" the date in the dialog
    private DatePickerDialog.OnDateSetListener mDateSetListener =
            new DatePickerDialog.OnDateSetListener() {

                public void onDateSet(DatePicker view, int year, 
                                      int monthOfYear, int dayOfMonth) {
                    mYear = year;
                    mMonth = monthOfYear;
                    mDay = dayOfMonth;
                    updateDisplay();
                }
            };
            
            @Override
            protected Dialog onCreateDialog(int id) {
                switch (id) {
                case DATE_DIALOG_ID:
                    return new DatePickerDialog(this,
                                mDateSetListener,
                                mYear, mMonth, mDay);
                case TIME_DIALOG_ID:
                    return new TimePickerDialog(this,
                            mTimeSetListener, mHour, mMinute, false);
                
                }
                
                return null;
            }                


	@Override
	public void onClick(View v) {

		
	}
	
}

